﻿namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            using (var ctx = new TestDbContext())
            {
                //AddBook(ctx).Wait();
                //AddBooks(ctx).Wait();
                //QueryBooks(ctx).Wait();
                AddBook1(ctx).Wait();
                //UpdateBook1(ctx).Wait();
                DeleteBook1(ctx).Wait();
            }
        }

        static async Task DeleteBook1(TestDbContext ctx)
        {
            var b = ctx.Books.Single(b => b.Title == "数学之美（第3版）");
            ctx.Remove(b);
            await ctx.SaveChangesAsync();
        }

        static async Task UpdateBook1(TestDbContext ctx)
        {
            var b = ctx.Books.Single(b => b.Title == "数学之美（第3版）");
            b.AuthorName = "Jun Wu";
            await ctx.SaveChangesAsync();
        }

        static async Task AddBook1(TestDbContext ctx)
        {
            Book book = new Book()
            {
                Title = "数学之美（第3版）",
                AuthorName = "吴军",
                Price = 88,
                PubTime = new DateTime(2012, 1, 1)
            };
            ctx.Books.Add(book);
            await ctx.SaveChangesAsync();

            Console.WriteLine(book.Id); //自增主键
        }

        static async Task QueryBooks(TestDbContext ctx)
        {
            IQueryable<Book> books = ctx.Books.Where(b => b.Price > 80);
            foreach(var book in books)
            {
                Console.WriteLine(book.Title+", "+book.Price);
            }

            Book oneBook = ctx.Books.Single(b => b.Title == "架构整洁之道");
            Console.WriteLine(oneBook.AuthorName);

            IQueryable<Book> books1 = ctx.Books.Where(b => b.Price > 80)
                .OrderBy(b => b.Price);
            foreach (var book in books1)
            {
                Console.WriteLine(book.Title + ", " + book.Price);
            }

            var groups = ctx.Books.GroupBy(b => b.AuthorName)
                .Select(g => new {
                    AuthorName = g.Key,
                    BooksCount = g.Count(),
                    MaxPrice = g.Max(b => b.Price)
                });
            foreach (var g in groups)
                Console.WriteLine($"作者名:{g.AuthorName},著作数量:{g.BooksCount},最贵的价格:{g.MaxPrice}");
        }

        static async Task AddBooks(TestDbContext ctx)
        {
            Book book = new Book()
            {
                Title = "架构整洁之道",
                AuthorName = "罗伯特.马丁",
                Price = 128,
                PubTime = new DateTime(2018, 9, 1)
            };
            ctx.Books.Add(book);
            Book book1 = new Book()
            {
                Title = "敏捷软件开发（珍藏版）",
                AuthorName = "罗伯特.马丁",
                Price = 108,
                PubTime = new DateTime(2021, 1, 1)
            };
            ctx.Books.Add(book1);
            Book book2 = new Book()
            {
                Title = "实现领域驱动设计",
                AuthorName = "沃恩.佛农",
                Price = 68,
                PubTime = new DateTime(2014, 3, 1)
            };
            ctx.Books.Add(book2);
            await ctx.SaveChangesAsync();
        }

        static async Task AddBook(TestDbContext ctx)
        {
            Book book = new Book()
            {
                Title = "UML和模式应用（原书第3版）",
                AuthorName = "拉曼",
                Price = 88,
                PubTime = new DateTime(2006, 5, 1)
            };
            ctx.Books.Add(book);
            await ctx.SaveChangesAsync();
        }

    }
}
